public class ReversePrint {

    public int[] reversePrint(ListNode head) {
        if (head == null) return new int[]{};
        ListNode dummyHead = new ListNode(0);
        ListNode temp = head;
        int count = 0;
        while (temp != null) {
            ListNode next = temp.next;
            temp.next = dummyHead.next;
            dummyHead.next = temp;
            temp = next;
            count++;
        }
        int[] ans = new int[count];
        int i = 0;
        temp = dummyHead.next;
        while (temp != null) {
            ans[i] = temp.val;
            temp = temp.next;
            i++;
        }
        return ans;
    }
}


class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    ListNode() {}

}
